<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-09-13 21:15:55
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-10-11 22:34:01
-->
<template>
  <a-modal
    :open="visible"
    :keyboard="false"
    :maskClosable="false"
    :title="title"
    @cancel="onCancel"
    @ok="onSubmit"
    :confirmLoading="submitLoading"
    :width="900"
  >
    <div style="height: 550px; overflow: auto">
      <a-form ref="formRef" v-bind="formLayout" :model="model" :rules="rules">
        <a-form-item label="分类编码">
          <a-input v-model:value="model.id"> </a-input>
        </a-form-item>
        <a-form-item label="分类名称">
          <a-input v-model:value="model.name"> </a-input>
        </a-form-item>
        <a-form-item label="配置用途">
          <enum-select v-model:value="model.usage" name="Usage" />
        </a-form-item>
        <a-form-item label="配置级别">
          <enum-select
            v-model:value="model.level"
            :flag="true"
            name="ConfigLevel"
          />
        </a-form-item>
        <a-form-item label="配置字段定义">
          <ConfigFields :fields="model.defineFields" />
        </a-form-item>
        <a-form-item label="启用状态">
          <a-switch v-model:checked="model.status"></a-switch>
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-model:value="model.remarks"></a-input>
        </a-form-item>
      </a-form>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
import { ConfigCategoryDto, ConfigDefineDto } from "@/api/system/types";
import { useFormPage } from "@/helpers/pages";
import { FormInstance } from "ant-design-vue";
import { computed, onMounted, ref, watch } from "vue";
import api from "@/api/system/configCategory";
import ConfigFields from "./ConfigFields.vue";
const formRef = ref<FormInstance>();
const emits = defineEmits(["update:visible", "close", "success"]);
const props = defineProps({
  id: String,
  visible: { type: Boolean, default: false },
  action: {
    type: String,
    default: "create",
  },
  item: {
    type: Object,
    default() {
      return {};
    },
  },
});

const { model, rules, formLayout, loading, submitLoading } = useFormPage<
  ConfigCategoryDto,
  string
>({
  formLayout: {
    labelCol: { span: 3 },
    wrapperCol: { span: 20 },
  },
  model: {
    id: "",
    level: 1,
    name: "",
    moduleScope: [],
    configType: 0,
    usage: 1,
    remarks: "",
    status: true,
    defineFields: [],
  },
});

const isEdit = computed(() => {
  return props.action === "edit";
});
 
/**
 * 弹窗标题
 */
const title = computed(() => {
  return isEdit.value ? "修改配置定义信息" : "添加配置定义信息";
});

/**
 * 加载数据
 */
const loadData = () => {
  if (isEdit.value) {
    loading.value = true;
    api.get(props.id).then((res) => {
      Object.assign(model, res);
      loading.value = false;
    });
  }
};

/**
 * 提交
 */
const onSubmit = () => {
  emits("success");
  formRef.value.validate().then(() => {
    let request = isEdit.value
      ? api.update(props.id, model)
      : api.create(model);
    submitLoading.value = true;
    request.then((res) => {
      emits("success", res);
      emits("update:visible", false);
      emits("close");
    })
      .finally(() => {
        submitLoading.value = false;
      });
  });
};

/**
 * 取消
 */
const onCancel = () => {
  emits("update:visible", false);
  emits("close");
};

watch(
  () => props.visible,
  (val: boolean) => {
    console.log("visible changed,val,", val);
    val && loadData();
  },
  { immediate: true }
);
</script>

<style lang="less" scoped>
.row {
  .cell {
    padding: 5px 8px;
  }

  &-header {
    background: rgba(0, 0, 0, 0.05);
    border-bottom: 1px solid #eee;
    .cell {
      font-weight: bold;
    }
  }

  &-footer {
    padding: 5px 8px;
  }
}
</style>
